home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Input into array of strings?
- Date: 9 Mar 1996 18:12:17 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4ht38h$d1@umbc9.umbc.edu>
- References: <4hnv1e$o7n@rhea.glo.be>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Sven Claeys <sclaeys@glo.be> wrote:
- |> I've got a some kind of problem reading into arrays of strings. I'm a
- |> bit rusty in C or C++, it's been a while, so please be patient with
- |> me.
- |>
- |> here we go:
- |>
- |> i've got a structure
- |>
- |> struct ImmoData
- |> {
- |> char code[3][5];
-
- Note that this can be treated as either a 3x5 2D array of char or an
- array of 3 strings of length 4 which are NUL terminated.
- |> ....
- |> }....;
- |>
- |> now i want to read data from a file with fgets(...) into this array.
- |>
- |> I've tried
- |> for (i=0; i<5; i++)
- |> fgets(code[i],....);
-
- Well there's the problem. You only have 3 array elements so 5 is incorrect.
- Make it:
-
- for (i = 0; i < 3; i++)
- fgets (code[i], 5, myinputstream);
-
- |> or
- |> for (i=0; i<5; i++)
- |> fgets((char*) code[i], ...);
-
- Same as above except the cast is unnecessary.
-
- |> but none of these seem to work. As i'm debugging the first code is
- |> read correct, but the second is appended to the first and is also read
- |> into the second, and so on.
- |>
- |> Could anyone help me with this strange phenomenon?
-
- A phenomenon? Nothing so fancy...Just accessing memory which is not
- properly designated for your use which produces undefined results.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-